package com.yc.demo.demo;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/loginWithVcode.s")
public class LoginWithVcodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		// 获取会话中的验证码
		String vcode = (String) session.getAttribute("vcode");
		// 获取用户提交的验证码
		String rcode = request.getParameter("rcode");
		if(vcode.equals(rcode)) {
			String json = "{ \"code\" : \"1\", \"msg\" : \"正确\"}";
			response.getWriter().append(json);
		} else {
			String json = "{ \"code\" : \"0\", \"msg\" : \"验证码错误！\"}";
			response.getWriter().append(json);
		}
		/**
		 * 不判断用户名密码， 直接登录成功，用于测试 监听器 
		 */
		String name = request.getParameter("name");
		// 该行代码直接触发 会话属性监听器的  addXXX 方法
		request.getSession().setAttribute("loginedUser", name);
		
		
		
		
		
		
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
